package cn.yx.common.mdc.configuration;

import cn.yx.common.mdc.spring.MdcDecoratorThreadPoolTaskExecutor;
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;

/**
 * <p>自定义的SpringBoot异步任务线程池，支持MDC上下文信息。</p>
 * <p>使用@Async注解时，异步任务中加入MDC上下文信息.</p>
 * <p>使用了SpringBoot自带的TaskExecutionProperties自动化配置类。spring.task.execution</p>
 *
 * @author Wgssmart
 */
@Configuration
@EnableConfigurationProperties(TaskExecutionProperties.class)
@EnableAsync
public class MdcAsyncTaskConfiguration extends AsyncConfigurerSupport {

    private final TaskExecutionProperties taskExecutionProperties;

    public MdcAsyncTaskConfiguration(TaskExecutionProperties taskExecutionProperties) {
        this.taskExecutionProperties = taskExecutionProperties;
    }

    @Override
    public Executor getAsyncExecutor() {
        return new MdcDecoratorThreadPoolTaskExecutor(taskExecutionProperties);
    }

}